package c926.xiaoduyun.xiaodu.mservice;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.util.CollectionUtils;
import kd.sdk.plugin.Plugin;

import java.util.*;

/**
 * 动态表单插件
 */
public class XiaoduFanganFormPlugin extends AbstractFormPlugin implements Plugin {


    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok");
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        Map<String, Object> customParams = formShowParameter.getCustomParams();
        List<String> orgList = (List<String>) customParams.getOrDefault("c926_orgno", new ArrayList<String>());
        DynamicObjectCollection entity = this.getModel().getDataEntity(true).getDynamicObjectCollection("c926_entryentity");
        if (CollectionUtils.isNotEmpty(orgList)) {
            QFilter qFilter = new QFilter("number", QCP.in, orgList);
            DynamicObject[] orgs = BusinessDataServiceHelper.load("bos_org", "id", qFilter.toArray());
            for (int i = 0; i < orgList.size(); i++) {
                DynamicObject addNew = entity.get(0);
                addNew.set("c926_org", orgs[i]);
            }
            this.getView().updateView("c926_entryentity");
        }
        Long parentId = (Long) customParams.getOrDefault("parent_id", 0L);
        this.getView().getPageCache().put("parent_id", String.valueOf(parentId));
    }


    @Override
    public void beforeClick(BeforeClickEvent evt) {
        super.beforeClick(evt);
        String key = ((Control) evt.getSource()).getKey();
        if ("btnok".equals(key)) {
            DynamicObject c926Org = (DynamicObject) this.getModel().getValue("c926_org");
            if (Objects.isNull(c926Org)) {
                this.getView().showTipNotification("请选择组织");
                evt.setCancel(true);
            }
            //查询当前车间是否有匹配的消毒方案
            Long orgId = (Long) c926Org.get("id");
            QFilter qFilter = new QFilter("c926_orgfield", QCP.equals, orgId).and("status", QCP.equals, "A");
            DynamicObject loaded = BusinessDataServiceHelper.loadSingle("c926_scheme", qFilter.toArray());
            if (null != loaded) {
                this.getView().showTipNotification("该车间已有最新方案，请重新选择！");
                evt.setCancel(true);
            }

        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        if ("btnok".equals(key)) {
            Long parentId = Long.parseLong(this.getPageCache().get("parent_id"));
            DynamicObject c926Org = (DynamicObject) this.getModel().getValue("c926_org");
            DynamicObject scheme = BusinessDataServiceHelper.loadSingle(parentId, "c926_scheme");
            scheme.set("c926_orgfield", c926Org);
            SaveServiceHelper.save(new DynamicObject[]{scheme});
            this.getView().close();

        }
    }
}